package com.crr.mall.ware.service.impl;

import com.baomidou.mybatisplus.annotation.TableName;
import com.crr.mall.common.utils.Assert;
import com.crr.mall.common.utils.R;
import com.crr.mall.ware.dao.PurchaseDetailDao;
import com.crr.mall.ware.dao.WareSkuDao;
import com.crr.mall.ware.entity.PurchaseDetailEntity;
import com.crr.mall.ware.entity.WareSkuEntity;
import com.crr.mall.ware.feign.ProductFeignService;
import com.crr.mall.ware.service.PurchaseDetailService;
import com.crr.mall.ware.vo.Items;
import com.crr.mall.ware.vo.MergeVO;
import com.crr.mall.ware.vo.PurchaseDoneVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crr.mall.common.utils.PageUtils;
import com.crr.mall.common.utils.Query;

import com.crr.mall.ware.dao.PurchaseDao;
import com.crr.mall.ware.entity.PurchaseEntity;
import com.crr.mall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {
    @Autowired
    PurchaseDetailDao purchaseDetailDao;
    @Autowired
    PurchaseDetailService purchaseDetailService;
    @Autowired
    WareSkuDao wareSkuDao;
    @Autowired
    ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryByCondition(Map map) {
        int page = Integer.parseInt(map.get("page").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        PageHelper.startPage(page, pageSize);
        List<Map> list = baseMapper.queryByCondition(map);
        PageInfo info = new PageInfo(list);
        return new PageUtils(list, (int) info.getTotal(), pageSize, page);

    }

    @Transactional
    @Override
    public int mergePurchase(MergeVO mergeVO) {
        if (null == mergeVO.getPurchaseId()) {
            //新建一个采购单
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setStatus(0);
            int i = baseMapper.savePurchase(purchaseEntity);
            Assert.fail(i == 1, "新增采购单成功");
            //修改采购详情表 绑定采购单id并修改状态
            int i1 = purchaseDetailDao.updatePidAndStatus(purchaseEntity.getId(), mergeVO.getItems());
            Assert.fail(i1 >= 1, "修改采购详情成功");
        } else {
            //直接修改采购详情表 绑定采购单id并修改状态
            int i1 = purchaseDetailDao.updatePidAndStatus(mergeVO.getPurchaseId(), mergeVO.getItems());
            Assert.fail(i1 >= 1, "修改采购详情成功");
        }

        return 1;
    }

    @Transactional
    @Override
    public int recivePurchase(List<Long> ids) {
        //找到 这些采购单确认 状态为1 和0 的采购单 直接查询
        List<PurchaseEntity> purchase = baseMapper.findPurchase(ids);
        //修改采购单状态为 2已领取
        List<Long> IDS = new ArrayList<>();
        purchase.forEach(p -> {
            int i = baseMapper.updateStatus(p.getId());
            Assert.fail(i == 1, "修改采购单状态成功");
            IDS.add(p.getId());
        });
        //修改采购详情单状态 2 正在采购
        int i = purchaseDetailDao.updateStatus(IDS);
        Assert.fail(i >= 1, "修改采购单的采购详情状态成功");

        return 1;
    }

    @Transactional
    @Override
    public int purchaseFinish(PurchaseDoneVO purchaseDoneVO) {

        //1先查看所有采购单详情的状态是否已经全部采购完成 有一个未完成则采购单状态就未完成
        boolean flag = true;//默认标志所有采购单都已经完成  3完成 4 异常
        List<Items> items = purchaseDoneVO.getItems();
        List<PurchaseDetailEntity> detailList = new ArrayList<>();
        for (int i = 0; i < items.size(); i++) {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            if (items.get(i).getStatus() == 4) {
                flag = false;
                purchaseDetailEntity.setStatus(4);
            } else {
                purchaseDetailEntity.setStatus(3);
                //将成功采购的入库
                //根据itemid查询当前采购详情单的skuId wareId skuNum 存进wareSku
                //先判断wareSku是否存在 存在则为修改 不存在则为新增

                PurchaseDetailEntity purchaseDetail = purchaseDetailService.getById(items.get(i).getItemId());
                WareSkuEntity entity = wareSkuDao.getByskuIdAndwareId(purchaseDetail);
                if (null == entity) {
                    //新增
                    //远程调用renrenmall-product 查询skuName
                    String skuName = null;
                    try {
                        R info = productFeignService.info(entity.getSkuId());
                        Map<String, Object> map = (Map) info.get("sku");
                        int code = (int) info.get("code");
                        if (code == 0) {
                         skuName = (String) map.get("skuName");
                        }
                    } catch (Exception e) {

                    }

                    int save = wareSkuDao.saveStock(skuName,purchaseDetail);
                    Assert.fail(save == 1, "修改库存成功");
                } else {
                    //修改
                    int update = wareSkuDao.addStock(purchaseDetail);
                    Assert.fail(update == 1, "修改库存成功");
                }

            }
            purchaseDetailEntity.setId(items.get(i).getItemId());
            detailList.add(purchaseDetailEntity);

        }
        //批量更新detail
        boolean b = purchaseDetailService.updateBatchById(detailList);
        Assert.fail(b, "批量更新采购详情单失败");
        //如果有false 则更新采购单状态 4 有异常 否则3完成
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(purchaseDoneVO.getId());
        purchaseEntity.setStatus(flag ? 3 : 4);
        boolean b1 = this.updateById(purchaseEntity);
        Assert.fail(b1, "更新采购单失败");
        //将成功采购的入库
        //根据itemid查询当前采购详情单的skuId wareId skuNum 存进wareSku

        return 1;
    }

}